package com.ittable.tableshow.controller;

import com.ittable.tableshow.mapper.DatasetMapper;
import com.ittable.tableshow.mapper.TableMapper;
import com.ittable.tableshow.pojo.DatasetWithTables;
import com.ittable.tableshow.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/table")
public class TableNamesController {
    @Autowired
    private TableMapper tableMapper;
    @Autowired
    private DatasetMapper datasetMapper;

    @GetMapping("/all")
    public Result<List<DatasetWithTables>> showAllTablesByDataset() {
        List<String> allDatasetNames = datasetMapper.getAllDatasetNames();

        List<DatasetWithTables> datasetWithTablesList = new ArrayList<>();
        for (String datasetName : allDatasetNames) {
            List<String> tableNames = tableMapper.getTablesByDatasetName(datasetName);
            datasetWithTablesList.add(new DatasetWithTables(datasetName, tableNames));
        }

        return Result.success(datasetWithTablesList);
    }
}
